/****************************************************************
 * <strong>MFWebGet</strong>
 *
 * <p>
 *MFWebGet
 * </p>
 *
 * @author AndyZhang 2011 cnet2001@163.com
 * @version 0.1
 * ToDo:2011-12-6
 ***************************************************************/

package com.thinkwide.data.exchange;

import com.thinkwide.data.core.MFPropperties;
import com.thinkwide.data.core.MFStringBuilder;
import com.thinkwide.data.debug.MFDebug;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

public class MFWebGet {

    public static String proxy_server = "proxy.server";
    public static String proxy_port = "proxy.port";

    public static String getWebHtml(String urlPath) {
        return getWebHtml(urlPath, "gbk");
    }

    public static String getWebHtmlByProxy(String urlPath, String chartSet) throws Throwable {

        MFPropperties prop = new MFPropperties("weather.properties", true);
        String server = prop.getPropValue(proxy_server);
        String port = prop.getPropValue(proxy_port);
        int iPort = Integer.parseInt(port);

        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL(urlPath);
            HttpURLConnection httpConn = null;
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(server, iPort));
            httpConn = (HttpURLConnection) url.openConnection(proxy);

            httpConn.setConnectTimeout(5000);
            httpConn.connect();

            //BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(),chartSet));
            BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), chartSet));
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }

            in.close();
        } catch (Exception e) {
            sb.append(e.toString());
            System.err.println(e);
        }
        return sb.toString();

    }

    public static String getWebHtml(String urlPath, String chartSet) {

        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL(urlPath);

            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), chartSet));
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }

            in.close();
        } catch (Exception e) {
            sb.append(e.toString());
            System.err.println(e);
        }
        return sb.toString();
    }

    public static Map<String, String> getWeaverWebHtmlMap(String data) {

        //LinkedHashMap<String,String> mapList=new LinkedHashMap<String, String>();
        data = data.replace("{", "");
        data = data.replace("}", "");

        Map<String, String> map = new LinkedHashMap<String, String>();
        for (String keyValue : data.split(",")) {
            String[] parts = keyValue.split("=", 2);


            map.put(parts[0].trim(), parts[1].trim());
        }
        return map;
    }

    public static String getDataLeft(String data) {
        String[] counts = data.split(":", 2);
        return counts[0].trim();
    }

    public static String getDataRight(String data) {
        String[] counts = data.split(":", 2);
        return counts[1].trim();
    }

    public static Map<String, String> getWeaverDocCountMap(String urlPath) {
        return getWeaverWebHtmlMap(getWebHtml(urlPath));
    }

    public static String getCommunicationData(String dataUrl) {

        String data = getWebHtml(dataUrl);
        return data;
    }

    public static String getCloudData(String dataUrl, String linkUrl) {
        String data = getWebHtml(dataUrl, "utf-8");
        MFDebug.println("data:" + data);
        data = data.replace("[", "");
        data = data.replace("]", "");

        String[] tags = data.split(",");

        MFStringBuilder sb = new MFStringBuilder();
        sb.println("<tags>");


        int index = 0;
        for (String item : tags) {
            String color = "";
            if (index < 3)
                color = "color='0xFF0000'";
            else
                color = "color='0x000000'";
            sb.println("<a href='%s' target='_blank' class='tag-link-66' rel='tag' " +
                    "style='font-size:14pt;font-style:oblique' %s " +
                    "hicolor='0xED511B'>%s</a>", linkUrl + item.trim(), color, item.trim());

            index++;
        }

        sb.println("</tags>");

        return sb.toString();
    }

}
